home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Talking Clock Pro™ 2.0.1 / Talking Clock Pro Source / Controller / Source / apprec.c next >
Encoding:
C/C++ Source or Header  |  1996-11-24  |  8.4 KB  |  421 lines  |  [TEXT/CWIE]

  1. /*
  2.  * apprec.c
  3.  */
  4.  
  5. #include "apprec.h"
  6. #include "menu.h"
  7. #include "x.h"
  8. #include "clockrec.h"
  9. #include "clwinrec.h"
  10. #include "util.h"
  11. #include <Speech.h>
  12.  
  13.  
  14. DefWindowRec appRec = {
  15.  
  16.     128 , NULL ,
  17.  
  18.     AppCr ,
  19.     AppDe ,
  20.     AppUp ,
  21.     AppMD ,
  22.     AppMU ,
  23.     AppKD ,
  24.     AppAK ,
  25.     AppAc ,
  26.     AppSw ,
  27.     AppId ,
  28.     AppPr ,
  29.     AppCo ,
  30.     AppAE
  31.  
  32. } ;
  33.  
  34.  
  35. long startedTicks = 60000 ;
  36.  
  37. #define CURRENT_VER 3
  38.  
  39. typedef struct AppData {
  40.  
  41.     FSSpec            theFile ;
  42.     Handle            theWindows ;
  43.  
  44. } AppData , * AppPtr ;
  45.  
  46.  
  47. static void
  48. AppAboutItem ( ) {
  49.     PicHandle ph;
  50.     Rect r;
  51.     EventRecord event;
  52.     WindowPtr about;
  53.  
  54.     if (!SpeechBusy()) {
  55.         Handle h = GetResource('STR ', 132);
  56.         HLock(h);
  57.         SpeakString((unsigned char *)*h);
  58.         ReleaseResource((Handle)h);
  59.     }
  60.     if (!ColorAvailable() || ((ph = (PicHandle)GetResource('PICT', 128)) == NULL)) {
  61.         Alert(129, NULL);
  62.         return;
  63.     }
  64.     HLockHi((Handle)ph);
  65.     r = (*ph)->picFrame;
  66.     OffsetRect(&r, 200, 100);
  67.     about = NewCWindow(NULL, &r, "\p", true, altDBoxProc, (WindowPtr)-1L, false, 0L);
  68.     SetPort(about);
  69.     DrawPicture(ph, &about->portRect);
  70.     while (!Button() && !OSEventAvail(keyDownMask, &event))
  71.         SystemTask();
  72.     FlushEvents(keyDownMask|mDownMask, 0);
  73.     CloseWindow(about);
  74.     ReleaseResource((Handle)ph);
  75. }
  76.  
  77.  
  78. static void
  79. AppAppleMenu ( short item , unsigned char * itemStr ) {
  80.  
  81.     if ( item == ABOUT_ITEM ) {
  82.         AppAboutItem ( ) ;
  83.     } else {
  84.         OpenDeskAcc ( itemStr ) ;
  85.     }
  86. }
  87.  
  88.  
  89. void
  90. ClWinAdd ( WindowPtr wp , Handle data ) {
  91.  
  92. AppPtr ptr = ( AppPtr ) * data ;
  93.  
  94.     PtrAndHand ( ( Ptr ) & wp , ptr -> theWindows , sizeof ( WindowPtr ) ) ;
  95. }
  96.  
  97.  
  98. void
  99. ClWinRemove ( WindowPtr wp , Handle data ) {
  100.  
  101. AppPtr ptr = ( AppPtr ) * data ;
  102. WindowPtr * wpp = ( WindowPtr * ) * ( ptr -> theWindows ) ;
  103. int num = GetHandleSize ( ptr -> theWindows ) / sizeof ( WindowPtr ) ;
  104. Boolean del = 0 ;
  105.  
  106.     while ( num -- ) {
  107.         if ( del ) {
  108.             wpp [ -1 ] = * wpp ;
  109.         } else {
  110.             if ( * wpp == wp ) {
  111.                 del = 1 ;
  112.             }
  113.         }
  114.         wpp ++ ;
  115.     }
  116.     SetHandleSize ( ptr -> theWindows , GetHandleSize ( ptr -> theWindows ) -
  117.         sizeof ( WindowPtr ) ) ;
  118. }
  119.  
  120.  
  121. static void
  122. AddPrefResource ( AppPtr ptr ) {
  123.  
  124. volatile Handle h = NULL ;
  125. volatile short refNum ;
  126.  
  127.     TRY {
  128.         h = GetResource ( 'STR ' , -16397 ) ; /* Magic prefs string */
  129.         FailNil ( h ) ;
  130.         DetachResource ( h ) ;
  131.         FSpCreateResFile ( & ( ptr -> theFile ) , 'pref' , 'pref' , smSystemScript ) ;
  132.         refNum = FSpOpenResFile ( & ( ptr -> theFile ) , fsRdWrPerm ) ;
  133.         if ( refNum < 1 ) {
  134.             FailErr ( ResError ( ) ) ;
  135.         }
  136.         AddResource ( h , 'STR ' , -16397 , "\p" ) ;
  137.         h = NULL ;
  138.         FailErr ( ResError ( ) ) ;
  139.     } CLEANUP {
  140.         if ( refNum > 0 ) {
  141.             CloseResFile ( refNum ) ;
  142.             refNum = 0 ;
  143.         }
  144.         if ( h ) {
  145.             DisposeHandle ( h ) ;
  146.         }
  147.     } DONE ;
  148. }
  149.  
  150.  
  151. void
  152. AppSaveWindows ( Handle data ) {
  153.  
  154. AppPtr ptr = ( AppPtr ) * data ;
  155. WindowPtr * wpp = ( WindowPtr * ) * ( ptr -> theWindows ) ;
  156. short num = GetHandleSize ( ptr -> theWindows ) / sizeof ( WindowPtr ) ;
  157. short refNum = 0 , ver ;
  158. long size ;
  159.  
  160.     TRY {
  161.         HLock ( ptr -> theWindows ) ;
  162.         FSpCreate ( & ( ptr -> theFile ) , 'pref' , 'pref' , smSystemScript ) ;
  163.         FailErr ( FSpOpenDF ( & ( ptr -> theFile ) , fsRdWrPerm , & refNum ) ) ;
  164.         FailErr ( SetEOF ( refNum , 0L ) ) ;
  165.         ver = CURRENT_VER ;
  166.         size = sizeof ( ver ) ;
  167.         FailErr ( FSWrite ( refNum , & size , ( Ptr ) & ver ) ) ;
  168.         size = sizeof ( num ) ;
  169.         FailErr ( FSWrite ( refNum , & size , ( Ptr ) & num ) ) ;
  170.         while ( num -- ) {
  171.             ClWinWrite ( refNum , * wpp ) ;
  172.             wpp ++ ;
  173.         }
  174.     } CLEANUP {
  175.         HUnlock ( ptr -> theWindows ) ;
  176.         if ( refNum ) {
  177.             FSClose ( refNum ) ;
  178.             refNum = 0 ;
  179.         }
  180.     } DONE ;
  181.     AddPrefResource ( ptr ) ;
  182. }
  183.  
  184.  
  185. static void
  186. AppFileMenu ( short item , Handle data ) {
  187.  
  188. WindowPtr wp ;
  189. extern Boolean hasController ;
  190.  
  191.     switch ( item ) {
  192.     case NEW_ITEM :
  193.         wp = MakeWindow ( NULL , & clwinRec ) ;
  194.         if ( wp ) {
  195.             SelectWindow ( wp ) ;
  196.             ShowWindow ( wp ) ;
  197.         }
  198.         break ;
  199.     case OPEN_ITEM :
  200.         if ( ! hasController ) {
  201.             wp = MakeWindow ( NULL , & clockRec ) ;
  202.             SelectWindow ( wp ) ;
  203.             ShowWindow ( wp ) ;
  204.         }
  205.         break ;
  206.     case QUIT_ITEM :
  207.         quit = 1 ;
  208.         break ;
  209.     }
  210. }
  211.  
  212.  
  213. static void
  214. ReadWindows ( FSSpec * fss ) {
  215.  
  216. volatile short refNum = 0 ;
  217. long size ;
  218. short num , ver ;
  219. WindowPtr wp ;
  220. volatile Boolean del = 0 ;
  221.  
  222.     TRY {
  223.         if ( ! FSpOpenDF ( fss , fsRdPerm , ( short * ) & refNum ) ) {
  224.             size = sizeof ( ver ) ;
  225.             FailErr ( FSRead ( refNum , & size , ( Ptr ) & ver ) ) ;
  226.             if ( ver != CURRENT_VER ) {
  227.                 Alert ( 132 , NULL ) ;
  228.                 del = 1 ;
  229.             }
  230.             if ( ! del ) {
  231.                 size = sizeof ( num ) ;
  232.                 FailErr ( FSRead ( refNum , & size , ( Ptr ) & num ) ) ;
  233.                 while ( num -- ) {
  234.                     wp = MakeWindow ( NULL , & clwinRec ) ;
  235.                     ClWinRead ( refNum , wp ) ;
  236.                     if ( wp ) {
  237.                         SelectWindow ( wp ) ;
  238.                         ShowWindow ( wp ) ;
  239.                     }
  240.                 }
  241.             }
  242.         }
  243.     } CLEANUP {
  244.         if ( refNum ) {
  245.             FSClose ( refNum ) ;
  246.             refNum = 0 ;
  247.         }
  248.     } DONE ;
  249.     if ( del ) {
  250.         FailErr ( FSpDelete ( fss ) ) ;
  251.     }
  252. }
  253.  
  254.  
  255. /*    This function will be called with a preferences file spec    */
  256. /*    The name of the prefs file is specified in STR id 1            */
  257. OSErr
  258. AppCr ( WindowPtr wp , Handle * data , FSSpec * file ) {
  259.  
  260.     startedTicks = TickCount ( ) + 180L ;
  261.     * data = NewHandle ( sizeof ( AppData ) ) ;
  262.     FailNil ( * data ) ;
  263.     HLockHi ( * data ) ;
  264.     ( ( AppPtr ) * * data ) -> theFile = * file ;
  265.     ( ( AppPtr ) * * data ) -> theWindows = NewHandle ( 0L ) ;
  266.  
  267.     ReadWindows ( file ) ;
  268.  
  269.     return noErr ;
  270. }
  271.  
  272.  
  273. /*    This is a good place to write back your preferences; it is    */
  274. /*    called right before the application quits                    */
  275. OSErr
  276. AppDe ( WindowPtr wp , Handle data ) {
  277.  
  278.     return noErr ;
  279. }
  280.  
  281.  
  282. /*    Update events for windows that don't handle them themselves    */
  283. /*    are sent here.                                                */
  284. OSErr
  285. AppUp ( WindowPtr wp , Handle data , EventRecord * event ) {
  286.     return noErr ;
  287. }
  288.  
  289.  
  290. /*    MouseDown events in window content that are not handled by    */
  291. /*    windows are sent here                                        */
  292. OSErr
  293. AppMD ( WindowPtr wp , Handle data , EventRecord * event ) {
  294.     return noErr ;
  295. }
  296.  
  297.  
  298. /*    MouseUp events in window content that are not handled by    */
  299. /*    windows are sent here                                        */
  300. OSErr
  301. AppMU ( WindowPtr wp , Handle data , EventRecord * event ) {
  302.     return noErr ;
  303. }
  304.  
  305.  
  306. /*    KeyDown events in window content that are not handled by    */
  307. /*    windows are sent here                                        */
  308. OSErr
  309. AppKD ( WindowPtr wp , Handle data , EventRecord * event ) {
  310.     return noErr ;
  311. }
  312.  
  313.  
  314. /*    AutoKey events in window content that are not handled by    */
  315. /*    windows are sent here                                        */
  316. OSErr
  317. AppAK ( WindowPtr wp , Handle data , EventRecord * event ) {
  318.     return noErr ;
  319. }
  320.  
  321.  
  322. /*    Activate events for windows that are not handled by the        */
  323. /*    window are sent here                                        */
  324. OSErr
  325. AppAc ( WindowPtr wp , Handle data , EventRecord * event ) {
  326.     return noErr ;
  327. }
  328.  
  329.  
  330. /*    MultiFinder events for windows that are not handled by the    */
  331. /*    window are sent here                                        */
  332. OSErr
  333. AppSw ( WindowPtr wp , Handle data , EventRecord * event ) {
  334.  
  335. extern Boolean hasController ;
  336.  
  337.     if ( ( ! FrontWindow ( ) ) || ( TickCount ( ) > startedTicks ) ) {
  338.         if ( ( event -> message & 1 ) && ! hasController ) {
  339.             wp = MakeWindow ( NULL , & clockRec ) ;
  340.             SelectWindow ( wp ) ;
  341.             ShowWindow ( wp ) ;
  342.         }
  343.     }
  344.     return noErr ;
  345. }
  346.  
  347.  
  348. /*    When the front window does not handle idling, this is        */
  349. /*    called                                                        */
  350. OSErr
  351. AppId ( WindowPtr wp , Handle data , long * sleep ) {
  352.  
  353. AppPtr ptr = ( AppPtr ) * data ;
  354. WindowPtr * wpp = ( WindowPtr * ) * ( ptr -> theWindows ) ;
  355. int num = GetHandleSize ( ptr -> theWindows ) / sizeof ( WindowPtr ) ;
  356. DateTimeRec dtr ;
  357. unsigned long now ;
  358.  
  359.     GetDateTime ( & now ) ;
  360.     Secs2Date ( now , & dtr ) ;
  361.     HLock ( ptr -> theWindows ) ;
  362.     TRY {
  363.         while ( num -- ) {
  364.             if ( * wpp ) {
  365.                 ClWinRunClock ( * wpp , sleep , dtr . minute , dtr . second ) ;
  366.             }
  367.             wpp ++ ;
  368.         }
  369.     } CLEANUP {
  370.         HUnlock ( ptr -> theWindows ) ;
  371.     } DONE ;
  372.  
  373.     return noErr ;
  374. }
  375.  
  376.  
  377. /*    This is called when menus need to be updated.                */
  378. OSErr
  379. AppPr ( WindowPtr wp , Handle data ) {
  380.  
  381. extern Boolean hasController ;
  382.  
  383.     EnableCmd ( APPLE_MENU , ABOUT_ITEM ) ;
  384.  
  385.     EnableCmd ( FILE_MENU , NEW_ITEM ) ;
  386.     if ( ! hasController ) {
  387.         EnableCmd ( FILE_MENU , OPEN_ITEM ) ;
  388.     }
  389.     EnableCmd ( FILE_MENU , QUIT_ITEM ) ;
  390.  
  391.     return noErr ;
  392. }
  393.  
  394.  
  395. /*    This is called when the front window does not handle        */
  396. /*    commands                                                    */
  397. OSErr
  398. AppCo ( WindowPtr wp , Handle data , short menu , short item ,
  399.     unsigned char * itemStr ) {
  400.     switch ( menu ) {
  401.     case APPLE_MENU :
  402.         AppAppleMenu ( item , itemStr ) ;
  403.         break ;
  404.     case FILE_MENU :
  405.         AppFileMenu ( item , data ) ;
  406.         break ;
  407.     }
  408.     return noErr ;
  409. }
  410.  
  411.  
  412. /*    This is called when the front window does not handle        */
  413. /*    AppleEvents                                                    */
  414. OSErr
  415. AppAE ( WindowPtr wp , Handle data , AppleEvent * event ,
  416.     AppleEvent * reply ) {
  417.     return noErr ;
  418. }
  419.  
  420.  
  421.